import scrapy
import json
from xiecheng.MyDbUtil import MyDbUtil
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings


class XcstartSpider(scrapy.Spider):
    name = 'xcstart'

    cityName = ''
    cityPY = ''
    cityId = ''
    cityCode = ''
    page = 1
    hotelarea = 0
    cookies_str = '_abtest_userid=d17eb6d6-6036-4076-872c-864e99bc1b33; GUID=09031076411680500334; _ga=GA1.2.2100953454.1572273710; magicid=pH6+eNLzYCtKt1SdqAJWRso4hk5f2i1FwvQ4rwZ9zyHBaLSQv4yIN4/TI76Mhhde; _RSG=IJKatY8JFG7Eqg8Bwc8g.8; _RDG=281b4ff95d43542ef71fd6c2d992c56329; _RGUID=740f4c88-b791-44d2-8faa-226e8b22ad0e; MKT_Pagesource=PC; appFloatCnt=3; _RF1=117.143.97.194; manualclose=1; Session=smartlinkcode=U130026&smartlinklanguage=zh&SmartLinkKeyWord=&SmartLinkQuary=&SmartLinkHost=; Union=AllianceID=4897&SID=130026&OUID=&Expires=1575207004987; StartCity_Pkg=PkgStartCity=2; ASP.NET_SessionId=zgqrg4q5kpfwpk3aheem13ae; hoteluuid=NP9oVgOhllcH28UG; OID_ForOnlineHotel=15632906857441m0o5wk1574602644532102002; fcerror=538774225; _zQdjfing=d5c0865fa4cc3a923a3165bb3165bb186ad93365ac3365acd5c086; _HGUID=WTP%06T%03XXM%02WYQMTT%04RMX%06%01%01MRRV%05X%02RR%01%04P%05; HotelDomesticVisitedHotels1=27954358=0,0,4.6,328,/200q10000000ot65kC301.jpg,; _gid=GA1.2.682260332.1574951330; login_type=0; AHeadUserInfo=VipGrade=0&VipGradeName=%C6%D5%CD%A8%BB%E1%D4%B1&UserName=&NoReadMessageCount=0; DUID=u=17271C069CA6C826BD565FE87E48A8EF&v=0; IsNonUser=F; UUID=467BD574187347228E23D67C32C3312A; IsPersonalizedLogin=T; login_uid=66B63292A23110E4272CA814A67B879C; cticket=C61E27E3F3FE33E8276C3AB07407DC38DD42BB51DADDD5C13C28C7A6A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fkEbGx13jmrUpybsW7GEMHy5sKolxz4xFUJUOKHFiBPwN3rQGKc5E1MxBnjorhAyFlWfzE8SyNnKXrqpiblYFHIO6WArSmwf8WzbxacxzSE8XENDE5BWmFeBnwzhENajgqeU7ioAYTgrAzePde69y8hKTHi4Bw8DrkPKLFEA4xdtjBzeQ8vTSWO7EZSyhOKnFioByGFKX5vSSKs1EATE0OEcXWD9elGwqtEobjbHeFhi9dYLnrnlv9dv3djfbvB6wcXj3Ayh5EGHW5gEB8y9kKpHiq1yl1EcoWcYAYL6YdkibhihUiQnjQYzY5NE3Dyk6Wh1EXnYUTEGzvOmEoYmYZdR57Js1yQUE4bvMUJgmyFkE1ZWnBv4fjT0iUHitYkYpHWopY3Avlai4Bj0Dx48RPhKBZWdGWPqw0QvNcYn5jhfyMpj7YtYztR7OJD0w8ziUNYSZvDBW8bw01jFoJtXyHpEQ5; _bfa=1.1563290685744.1m0o5wk.1.1574951327143.1574957393303.9.49.0; _bfs=1.3; hotelhst=2012709687; _jzqco=%7C%7C%7C%7C%7C1.2132853145.1574597831857.1574957402225.1574957412643.1574957402225.1574957412643.0.0.0.17.17; __zpspc=9.7.1574957402.1574957412.2%232%7Cwww.baidu.com%7C%7C%7C%7C%23; _bfi=p1%3D102002%26p2%3D102002%26v1%3D49%26v2%3D48'
    cookies_dict = {i.split('=')[0]: i.split('=')[1] for i in cookies_str.split('; ')}

    def start_requests(self):
        mydb = MyDbUtil()
        rs = mydb.query_sql("select * from asp_area_index where area_Initials ='A'")
        for i in range(1, 4):
            thishref = "https://hotels.ctrip.com" + rs[i]['area_href']
            referserurl = thishref
            self.hotelarea = rs[i]['area_id']
            yield scrapy.Request(thishref, callback=self.parse,meta={"referserurl":referserurl})

    def parse(self, response):
        self.cityName = response.css("#cityName::attr(value)").extract_first()
        self.cityPY = response.css("#cityPY::attr(value)").extract_first()
        self.cityId = response.css("#cityId::attr(value)").extract_first()
        self.cityCode = response.css("#cityCode::attr(value)").extract_first()
        print(response.meta['referserurl'])
        yield scrapy.FormRequest(
            url="https://hotels.ctrip.com/Domestic/Tool/AjaxHotelList.aspx",
            callback=self.parse_detail,
            method='POST',
            headers= {
                'Referer': response.meta['referserurl'],
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
            },
            cookies=self.cookies_dict,
            # meta={"referserurl2": response.meta['referserurl']},
            formdata={
                'cityPY': self.cityPY,
                'cityId': self.cityId,
                'cityCode': self.cityCode,
                'cityName': self.cityName,
                'orderby': '3',
                'ordertype': '0',
                'page': '1',
            }
        )

    def parse_detail(self, response):
        pagecard = json.dumps(json.loads(response.body.decode("utf-8"))["hotelAmount"])
        pagecount = round(int(pagecard) / 25)
        # for currentPage in range(pagecount):
        #     yield scrapy.FormRequest(
        #         url="https://hotels.ctrip.com/Domestic/Tool/AjaxHotelList.aspx",
        #         method='POST',
        #         callback=self.parse_printdetail,
        #         headers={
        #             'Referer': response.meta['referserurl2'],
        #             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
        #         },
        #         cookies=self.cookies_dict,
        #         formdata={
        #             'cityPY': self.cityPY,
        #             'cityId': self.cityId,
        #             'cityCode': self.cityCode,
        #             'cityName': self.cityName,
        #             'orderby': '3',
        #             'ordertype': '0',
        #             'page': str(currentPage + 1),
        #         }
        #     )
        print(pagecard)

    # def parse_printdetail(self, response):
    #     datas = json.loads(response.body.decode("utf-8"))["hotelPositionJSON"]
    #     for index, sdd in enumerate(datas):
    #         print(index, sdd['name'])


if __name__ == '__main__':
    process = CrawlerProcess(get_project_settings())
    process.crawl('xcstart')
    process.start()
